package mysql

import "project/pkg/models"

func (m Mysql) LeaveCheckExist(id string) (exist bool, info *models.LeaveInfo, err error) {
	var count int64
	exist = false
	err = m.db.Table("leave_info").Where("id = ?", id).Count(&count).Error
	if err != nil {
		return
	}
	if count > 0 {
		exist = true
		err = m.db.Table("leave_info").Where("id = ?", id).First(&info).Error
		if err != nil {
			return
		}
	}
	return

}
func (m Mysql) LeaveUpdateInfoBuID(val *models.LeaveInfo) (err error) {
	return m.db.Table("leave_info").Save(&val).Error
}
func (m Mysql) LeaveDeleteByID(id string) (err error) {
	return m.db.Table("leave_info").Unscoped().Where("id = ?", id).Delete(&models.LeaveInfo{}).Error
}
func (m Mysql) LeaveQueryUserId(val string, page, size int) (res []*models.LeaveInfo, count int64, err error) {
	err = m.db.Table("leave_info").Where("request_id = ?", val).Offset((page - 1) * size).Limit(size).Count(&count).Order("created_at desc").Find(&res).Error
	return
}
func (m Mysql) LeaveQueryUserIdAndNo(val string, page, size int) (res []*models.LeaveInfo, count int64, err error) {
	err = m.db.Table("leave_info").Where("request_id = ? and action ='学生请求'", val).Offset((page - 1) * size).Limit(size).Count(&count).Order("created_at desc").Find(&res).Error
	return
}
func (m Mysql) LeaveQueryLeaveID(ID string) (res *models.LeaveInfo, err error) {
	err = m.db.Table("leave_info").Where("id = ?", ID).First(&res).Error
	return
}
